package MagnetoProject;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;


class DrawingPanel extends JPanel {

    final static int PANEL_WIDTH = 250;
    final static int PANEL_HEIGHT = 200;
    final static int THRESHOLD = 15;
    Storage store = new Storage();

    RedSquare redSquare = new RedSquare();


    public DrawingPanel() {
        //Setting the border to empty for now
        //setBorder(BorderFactory.createEmptyBorder());

        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.white);





        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });
    }


    void clearButtonActionPerformed(ActionEvent evt) {
        System.out.println("You're pressing the new clear button!");

        //this originally moves the square to the center of the panel, but current it moves it out of sight
      //  moveSquare(this.getCenterX() * 2, this.getCenterY() * 2);
        //this moves the square to the top right corner of the panel
        moveSquare(0,0);

        this.repaint();
    //if(Storage.getNumData() > 0)
     //   store.print();
    }

     private void moveSquare(int x, int y) {
         // Current square state, stored as final variables
        // to avoid repeat invocations of the same methods.
        final int CURR_X = redSquare.getX();
        final int CURR_Y = redSquare.getY();
        final int CURR_W = redSquare.getWidth();
        final int CURR_H = redSquare.getHeight();
        final int OFFSET = 1;

        if ((CURR_X!=x) || (CURR_Y!=y)) {

            int oldX = redSquare.getX();
            int oldY = redSquare.getY();

            redSquare.setX(x);
            redSquare.setY(y);

            int diffX = oldX - x;
            int diffY = oldY - y;

            diffX = abosulute(diffX);
            diffY = abosulute(diffY);

            Graphics g = getGraphics();

            if(diffX < THRESHOLD && diffY < THRESHOLD){
                g.setColor(Color.RED);
                g.drawLine(oldX, oldY, x, y);
          
            }




            // The square is moving, repaint background
            // over the old square location.
          // repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
            //repaint(0,0,2 + OFFSET, 2 + OFFSET);
/*
            // Update coordinates.
            redSquare.setX(x);
            redSquare.setY(y);

            // Repaint the square at the new location.
            repaint(redSquare.getX(), redSquare.getY(),
                    redSquare.getWidth()+OFFSET,
                    redSquare.getHeight()+OFFSET);
 **/
        }

    }


    public Dimension getPreferredSize() {
        return new Dimension(PANEL_WIDTH,PANEL_HEIGHT);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Draw Text
        // g.drawString("Draw to your hearts content",10,20);

       // redSquare.paintSquare(g);

        }

    public int getCenterX(){
    //EFFECT: this will return the X value of the co-ordinate for the center of the panel

        Dimension center = this.getSize();
        int centerX = (int) center.getWidth();
        centerX = centerX/2;

        return centerX;
    }

    public int getCenterY(){
    //EFFECT: this will return Y value of co-ordinate for the center of the panel

        Dimension center = this.getSize();
        int centerY = (int) center.getHeight();
        centerY = centerY/2;

        return centerY;

     }

    private int abosulute(int x){

        if (x < 0)
            x = -x;
        return x;
    }

}
